﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VisionarySystems.TimeRegi.Model.TimePeriods;

namespace VisionarySystems.TimeRegi.GUI
{
	public partial class Form_TimeSlotEdit : Form
	{
		public enum ChaneTypesEnum
		{
			Add,
			Edit
		}

		private TimeSlot m_EditedTimeSlot;

		public Form_TimeSlotEdit()
		{
			InitializeComponent();
			m_EditedTimeSlot = null;
		}

		public void Fill(TimeSlot Slot)
		{
			if (Slot != null) // Edit mode
			{
				this.Text = "Edit Timeslot";
				m_EditedTimeSlot = Slot;
				tb_Start.Text = m_EditedTimeSlot.Start.ToString();
				tb_Stop.Text = m_EditedTimeSlot.Stop.ToString();
			}
			else
			{
				this.Text = "Add Timeslot";
				m_EditedTimeSlot = null;
				tb_Start.Text = DateTime.Now.Date.AddHours(8).ToString();
				tb_Stop.Text = DateTime.Now.Date.AddHours(16).ToString();
			}
		}

		public TimeSlot EditedTimeSlot
		{
			get { return m_EditedTimeSlot; }
		}

		private void bu_OK_Click(object sender, EventArgs e)
		{
			bool convertSucces = true;
			try
			{
				if (m_EditedTimeSlot == null)
				{
					m_EditedTimeSlot = new TimeSlot(0, Convert.ToDateTime(tb_Start.Text), Convert.ToDateTime(tb_Stop.Text), TimeSlotTypes.WorkTime);
				}
				else
				{
					m_EditedTimeSlot.Start = Convert.ToDateTime(tb_Start.Text);
					m_EditedTimeSlot.Stop = Convert.ToDateTime(tb_Stop.Text);
				}
			}
			catch (Exception)
			{
				convertSucces = false;
			}
			if (convertSucces)
			{
				DialogResult = DialogResult.OK;
				Close();
			}
			else
				MessageBox.Show("Cant convert values");
		}

		private void bu_Cancel_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
			m_EditedTimeSlot = null;
			Close();
		}
	}
}
